package com.koron.operation.classes.form.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.koron.operation.classes.form.bean.convertor.ClassesFormRowConvertor;
import com.koron.operation.classes.form.bean.dto.ClassesFormRowDTO;
import com.koron.operation.classes.form.bean.po.ClassesFormRow;
import com.koron.operation.classes.form.mapper.ClassesFormRowMapper;
import com.koron.operation.classes.form.service.ClassesFormRowService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 月度排班职级表(ClassesFormRow)表服务实现类
 *
 * @author jzp
 * @since 2023-05-18 15:13:21
 */
@Service
public class ClassesFormRowServiceImpl implements ClassesFormRowService {

    @Autowired
    ClassesFormRowConvertor classesFormRowConvertor;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public ClassesFormRow getById(SessionFactory factory, String id) {
        ClassesFormRowMapper mapper = factory.getMapper(ClassesFormRowMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<ClassesFormRow> list(SessionFactory factory, ClassesFormRow classesFormRow) {
        ClassesFormRowMapper mapper = factory.getMapper(ClassesFormRowMapper.class);
        return mapper.selectList(classesFormRow);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ClassesFormRow classesFormRow) {
        ClassesFormRowMapper mapper = factory.getMapper(ClassesFormRowMapper.class);
        return mapper.insertOrUpdate(classesFormRow) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<ClassesFormRowDTO> rowList) {
        if (CollUtil.isEmpty(rowList)) {
            return false;
        }
        ClassesFormRowMapper mapper = factory.getMapper(ClassesFormRowMapper.class);
        List<ClassesFormRow> classesFormRows = classesFormRowConvertor.dtoListToEntityList(rowList);
        // 重写id
        boolean b = mapper.insertOrUpdateBatch(classesFormRows) > 0;
        for (int i = 0; i < classesFormRows.size(); i++) {
            rowList.get(i).setId(classesFormRows.get(i).getId());
        }
        return b;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ClassesFormRow classesFormRow) {
        ClassesFormRowMapper mapper = factory.getMapper(ClassesFormRowMapper.class);
        return mapper.update(classesFormRow) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        ClassesFormRowMapper mapper = factory.getMapper(ClassesFormRowMapper.class);
        return mapper.hidden(id) > 0;
    }

    @Override
    @TaskAnnotation("removeByFormId")
    public boolean removeByFormId(SessionFactory factory, String formId) {
        if (StrUtil.isEmpty(formId)) {
            return false;
        }
        ClassesFormRowMapper mapper = factory.getMapper(ClassesFormRowMapper.class);
        return mapper.removeByFormId(formId) > 0;
    }

}
